/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software Foundation,
 *    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package JET;

import java.io.File;
import java.io.IOException;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 *
 * @author 057Ry
 */
public class Utils
{
    public static boolean isJetProjectFile(String filename)
    {
        File file = new File(filename);

        String extension = getFileExtension(filename);

        if ( ! (extension != null && extension.equals("xml")) )
            return false;
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;

        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }

        Document doc = null;
        try {
            doc = db.parse(file);
        } catch (SAXException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }

        Element projectElem = doc.getDocumentElement();
        if (projectElem.getTagName().equals("project"))
            return true;

        /*
        NamedNodeMap attributes = projectElem.getAttributes();
        for (int i=0; i<attributes.getLength(); i++)
        {
            Node attrib = attributes.item(i);
            if (attrib.getNodeName().equals("app") &&
                attrib.getNodeValue().equals("JET")   )
                    return true;
        }
        */

        return false;
    }

    public static String getFileExtension(String path)
    {
        return (path.lastIndexOf(".")==-1)?"":path.substring(path.lastIndexOf(".")+1,path.length());
    }
    public static String switchFileExtensionTo(String path, String ext)
    {
        if (getFileExtension(path).equals(""))
        {
            return path+"."+ext;
        }
        else
        {
            return getFileDirectory(path)+getJustFileName(path)+"."+ext;
        }
    }
    public static String getFileNameFromPath(String path)
    {
        return (path.lastIndexOf('\\') == -1) ? path : path.substring(path.lastIndexOf('\\')+1, path.length());
    }
    
    public static String getFileDirectory(String path)
    {
        return (path.lastIndexOf('\\')==-1)? "" : path.substring(0, path.lastIndexOf('\\')+1);
    }
    public static String getJustFileName(String path)
    {
        String path_name = (path.lastIndexOf(".")==-1)? path : path.substring(0, path.lastIndexOf("."));
        return (path_name.lastIndexOf('\\') == -1) ? path_name : path_name.substring(path_name.lastIndexOf('\\')+1, path_name.length());
    }
}
